﻿using System;
using System.Collections.Generic;
using System.Text;
using StockTrader.FrontEnd.DataContract;
using System.Reflection;
using System.Data;
using System.ComponentModel;

namespace Mortgage.DAL
{
    public class Filter
    {
        public List<OrderUI> ordersFilter(List<OrderUI> list, string accountNo, string side, string symbol)
        {
            List<OrderUI> listFiltered = new List<OrderUI>();
            #region Filter
            if (list != null)
            {
                foreach (OrderUI order in list)
                {
                    if (!string.IsNullOrEmpty(accountNo))
                    {
                        if (!string.IsNullOrEmpty(side))
                        {
                            if (!string.IsNullOrEmpty(symbol))
                            {
                                if ((order.AccountNo.IndexOf(accountNo) > -1) && (order.Side.IndexOf(side) > -1) && (order.Symbol.IndexOf(symbol) > -1))
                                    listFiltered.Add(order);
                            }
                            else
                            {
                                if ((order.AccountNo.IndexOf(accountNo) > -1) && (order.Side.IndexOf(side) > -1))
                                    listFiltered.Add(order);
                            }
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(symbol))
                            {
                                if ((order.AccountNo.IndexOf(accountNo) > -1) && (order.Symbol.IndexOf(symbol) > -1))
                                    listFiltered.Add(order);
                            }
                            else
                            {
                                if (order.AccountNo.IndexOf(accountNo) > -1)
                                    listFiltered.Add(order);
                            }
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(side))
                        {
                            if (!string.IsNullOrEmpty(symbol))
                            {
                                if ((order.Side.IndexOf(side) > -1) && (order.Symbol.IndexOf(symbol) > -1))
                                    listFiltered.Add(order);
                            }
                            else
                            {
                                if (order.Side.IndexOf(side) > -1)
                                    listFiltered.Add(order);
                            }
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(symbol))
                            {
                                if (order.Symbol.IndexOf(symbol) > -1)
                                    listFiltered.Add(order);
                            }
                            else
                            {
                                listFiltered.Add(order);
                            }
                        }
                    }
                }
            }
            #endregion
            return listFiltered;
        }

        /// <summary>
        /// For buy transaction. Return the amount used based on matched result
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public decimal getSpentAmount(List<OrderUI> list)
        {
            List<OrderUI> listFiltered = new List<OrderUI>();
            decimal amount = 0;

            if (list != null)
            {
                foreach (OrderUI order in list)
                    if (order.Side.Equals("MUA") && (order.MVolume > 0))
                        amount += order.MValue;
            }

            return amount;
        }

        /// <summary>
        /// For buy transaction. Return the quantity does not match yet
        /// </summary>
        /// <param name="list"></param>
        /// <param name="accountNo"></param>
        /// <param name="symbol"></param>
        /// <returns></returns>
        public long getRemainBuyQttyBySymbol(List<OrderUI> list, string accountNo, string symbol)
        {
            List<OrderUI> listFiltered = new List<OrderUI>();
            long uMVolume = 0;

            if (list != null)
            {
                foreach (OrderUI order in list)
                {
                    if (order.AccountNo.Equals(accountNo) && order.Side.Equals("MUA") && order.Symbol.Equals(symbol) && (order.MVolume > 0))
                        uMVolume += (order.Volume - order.MVolume);
                }
            }

            return uMVolume;
        }


        /// <summary>
        /// For sell transaction. Return the amount based on matched result
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public decimal getNettingAmount(List<OrderUI> list)
        {
            List<OrderUI> listFiltered = new List<OrderUI>();
            decimal amount = 0;

            if (list != null)
            {
                foreach (OrderUI order in list)
                    if (!order.Side.Equals("MUA") && (order.MVolume > 0))
                        amount += order.MValue;
            }

            return amount;
        }

        /// <summary>
        /// For sell transaction. Return the quantity does not match yet
        /// </summary>
        /// <param name="list"></param>
        /// <param name="accountNo"></param>
        /// <param name="symbol"></param>
        /// <returns></returns>
        public long getRemainSellQttyBySymbol(List<OrderUI> list, string accountNo, string symbol)
        {
            List<OrderUI> listFiltered = new List<OrderUI>();
            long uMVolume = 0;

            if (list != null)
            {
                foreach (OrderUI order in list)
                {
                    if (order.AccountNo.Equals(accountNo) && !order.Side.Equals("MUA") && order.Symbol.Equals(symbol) && (order.MVolume > 0))
                        uMVolume += (order.Volume - order.MVolume);
                }
            }

            return uMVolume;
        }

        /// <summary>
        /// For sell transaction. Return the quantity of sell orders by symbol
        /// </summary>
        /// <param name="list"></param>
        /// <param name="accountNo"></param>
        /// <param name="symbol"></param>
        /// <returns></returns>
        public long getSellQttyBySymbol(List<OrderUI> list, string accountNo, string symbol)
        {
            List<OrderUI> listFiltered = new List<OrderUI>();
            long uMVolume = 0;

            if (list != null)
            {
                foreach (OrderUI order in list)
                {
                    if (order.AccountNo.Equals(accountNo) && !order.Side.Equals("MUA") && order.Symbol.Equals(symbol) && (order.MVolume > 0))
                        uMVolume += order.Volume;
                }
            }

            return uMVolume;
        }


        public List<OrderUI> ordersReport(List<OrderUI> list, string accountNo, string side, string symbol)
        {
            List<OrderUI> listFiltered = new List<OrderUI>();
            try
            {
                if (list != null)
                {
                    foreach (OrderUI order in list)
                    {
                        if (!string.IsNullOrEmpty(accountNo))
                        {
                            if (!string.IsNullOrEmpty(side))
                            {
                                if (!string.IsNullOrEmpty(symbol))
                                {
                                    if ((order.AccountNo.IndexOf(accountNo) > -1) && (order.Side.IndexOf(side) > -1) && (order.Symbol.IndexOf(symbol) > -1))
                                        if (order.MValue > 0)
                                            listFiltered.Add(order);
                                }
                                else
                                {
                                    if ((order.AccountNo.IndexOf(accountNo) > -1) && (order.Side.IndexOf(side) > -1))
                                        if (order.MValue > 0)
                                            listFiltered.Add(order);
                                }
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(symbol))
                                {
                                    if ((order.AccountNo.IndexOf(accountNo) > -1) && (order.Symbol.IndexOf(symbol) > -1))
                                        if (order.MValue > 0)
                                            listFiltered.Add(order);
                                }
                                else
                                {
                                    if (order.AccountNo.IndexOf(accountNo) > -1)
                                        if (order.MValue > 0)
                                            listFiltered.Add(order);
                                }
                            }
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(side))
                            {
                                if (!string.IsNullOrEmpty(symbol))
                                {
                                    if ((order.Side.IndexOf(side) > -1) && (order.Symbol.IndexOf(symbol) > -1))
                                        if (order.MValue > 0)
                                            listFiltered.Add(order);
                                }
                                else
                                {
                                    if (order.Side.IndexOf(side) > -1)
                                        if (order.MValue > 0)
                                            listFiltered.Add(order);
                                }
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(symbol))
                                {
                                    if (order.Symbol.IndexOf(symbol) > -1)
                                        if (order.MValue > 0)
                                            listFiltered.Add(order);
                                }
                                else
                                {
                                    if (order.MValue > 0)
                                        listFiltered.Add(order);
                                }
                            }
                        }
                    }
                }
                return listFiltered;
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        public List<OrderUI> ordersFault(List<OrderUI> list)
        {
            List<OrderUI> faultList = new List<OrderUI>();
            List<OrderUI> buyList = new List<OrderUI>();
            List<OrderUI> sellList = new List<OrderUI>();


            foreach (OrderUI order in list)
            {
                if (order.Side.Equals("MUA") && (!order.Status.Equals("X")))
                    buyList.Add(order);
                else if (order.Side.Equals("BÁN") && (!order.Status.Equals("X")))
                    sellList.Add(order);
            }

            foreach (OrderUI buy in buyList)
            {
                foreach (OrderUI sell in sellList)
                {
                    if (string.IsNullOrEmpty(buy.AccountNoChange))
                    {
                        if (string.IsNullOrEmpty(sell.AccountNoChange))
                        {
                            if (buy.AccountNo.Equals(sell.AccountNo) && buy.Symbol.Equals(sell.Symbol))
                            {
                                faultList.Add(buy);
                                faultList.Add(sell);
                            }
                        }
                        else
                        {
                            if (buy.AccountNo.Equals(sell.AccountNoChange) && buy.Symbol.Equals(sell.Symbol))
                            {
                                faultList.Add(buy);
                                faultList.Add(sell);
                            }
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(sell.AccountNoChange))
                        {
                            if (buy.AccountNoChange.Equals(sell.AccountNo) && buy.Symbol.Equals(sell.Symbol))
                            {
                                faultList.Add(buy);
                                faultList.Add(sell);
                            }
                        }
                        else
                        {
                            if (buy.AccountNoChange.Equals(sell.AccountNoChange) && buy.Symbol.Equals(sell.Symbol))
                            {
                                faultList.Add(buy);
                                faultList.Add(sell);
                            }
                        }
                    }
                }
            }

            return faultList;
        }
    }

}
